package com.candy.control;

import com.candy.bean.Car;
import com.candy.service.CarService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@Api("车辆信息管理")
@RestController
@RequestMapping("/car")
public class CarController {
    @Autowired
    CarService carService;
    /**
     * 接收get请求查询获取全部未删除的信息；进行分页处理
     * @param
     * @return
     */
    @GetMapping("/getallcars")
    public PageInfo<Car> getAllInfoes(Integer pageNum, Integer pageSize, Car car){

        PageHelper.startPage(pageNum,pageSize);

//        pageHelper.startPage下面的第一个查询会进行分页处理
        List<Car> lst=carService.getAllCars(car);
        PageInfo<Car> pageInfo=new PageInfo<>(lst);
        System.out.println("pagehelper:::::::::::::::::::");
        System.out.println("total:"+pageInfo.getTotal());
        System.out.println("size:"+pageInfo.getSize());
        System.out.println("list:"+pageInfo.getList());
        System.out.println("pageInfo:"+pageInfo);
        return pageInfo;
    }


    /**
     * 新增或编辑方法；影响行数大于0；数据更新成功返回ok；反之返回eg
     * @param car
     * @return
     */
    @RequestMapping("/addorupdatecar")
    public String addOrUpdateUser(@RequestBody  Car car){
        Integer num=carService.addOrUpdateCar(car);
        try {
            if(num>0){
                return "ok";
            }else {
                return "eg";
            }
        } catch (Exception e) {
            e.printStackTrace();
            return "";
        }

    }

    /**
     * 删除方法；影响行数大于0；数据更新成功返回ok；反之返回eg
     * @param car
     * @return
     */
    @RequestMapping("/deletecar")
    public String deleteUser(@RequestBody  Car car){
        Integer num=carService.deleteCar(car);
        if(num>0){
            return "ok";
        }else {
            return "eg";
        }

    }


}
